package com.ruoyi.APPMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.APPMS.domain.Part;
import com.ruoyi.APPMS.service.IPartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 零件Controller
 *
 * @author 任容
 * @date 2023-07-02
 */
@Api(tags = "零部件管理")
@RestController
@RequestMapping("/part/part")
public class PartController extends BaseController
{
    @Autowired
    private IPartService partService;

    /**
     * 分页查询零件列表
     */
    @ApiOperation("分页查询零件列表")
    @PreAuthorize("@ss.hasPermi('part:part:list')")
    @GetMapping("/list")
    public TableDataInfo list(Part part)
    {
        startPage();
//        QueryWrapper<Part> queryWrapper=new QueryWrapper<>();
       /* if(part.getStatus()!=null&&!"".equals(part.getStatus())){
            queryWrapper.like("status",part.getStatus());
        }
        if(part.getSupplierid()!=null&&!"".equals(part.getSupplierid())){
            queryWrapper.like("supplierid",part.getSupplierid());
        }
        if(part.getWarehouseid()!=null&&!"".equals(part.getWarehouseid())){
            queryWrapper.like("warehouseid",part.getWarehouseid());
        }
        if(part.getCustomerid()!=null&&!"".equals(part.getCustomerid())){
            queryWrapper.like("customerid",part.getCustomerid());
        }
        if(part.getSalespersonid()!=null&&!"".equals(part.getSalespersonid())){
            queryWrapper.like("salespersonid",part.getSalespersonid());
        }
        if(part.getPartname()!=null&&!"".equals(part.getPartname())){
            queryWrapper.like("partname",part.getPartname());
        }
        if(part.getType()!=null&&!"".equals(part.getType())){
            queryWrapper.like("type",part.getType());
        }*/
        List<Part> list = partService.list(part);
        return getDataTable(list);
    }

    /**
     * 导出零件列表
     */
    @ApiOperation("导出零件列表")
    @PreAuthorize("@ss.hasPermi('part:part:export')")
    @Log(title = "零件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Part part)
    {
        /*QueryWrapper<Part> queryWrapper=new QueryWrapper<>();
        if(part.getStatus()!=null&&!"".equals(part.getStatus())){
            queryWrapper.like("status",part.getStatus());
        }
        if(part.getSupplierid()!=null&&!"".equals(part.getSupplierid())){
            queryWrapper.like("supplierid",part.getSupplierid());
        }
        if(part.getWarehouseid()!=null&&!"".equals(part.getWarehouseid())){
            queryWrapper.like("warehouseid",part.getWarehouseid());
        }
        if(part.getCustomerid()!=null&&!"".equals(part.getCustomerid())){
            queryWrapper.like("customerid",part.getCustomerid());
        }
        if(part.getSalespersonid()!=null&&!"".equals(part.getSalespersonid())){
            queryWrapper.like("salespersonid",part.getSalespersonid());
        }
        if(part.getPartname()!=null&&!"".equals(part.getPartname())){
            queryWrapper.like("partname",part.getPartname());
        }
        if(part.getType()!=null&&!"".equals(part.getType())){
            queryWrapper.like("type",part.getType());
        }*/
        List<Part> list = partService.list(part);
        ExcelUtil<Part> util = new ExcelUtil<Part>(Part.class);
        util.exportExcel(response, list, "零件数据");
    }

    /**
     * 获取零件详细信息
     */
    @ApiOperation("获取零件详细信息")
    @PreAuthorize("@ss.hasPermi('part:part:query')")
    @GetMapping(value = "/{partid}")
    public AjaxResult getInfo(@PathVariable("partid") Long partid)
    {
        return success(partService.getById(partid));
    }

    /**
     * 新增零件
     */
    @ApiOperation("新增零件")
    @PreAuthorize("@ss.hasPermi('part:part:add')")
    @Log(title = "零件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Part part)
    {
        part.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(partService.save(part));
    }

    /**
     * 修改零件
     */
    @ApiOperation("修改零件")
    @PreAuthorize("@ss.hasPermi('part:part:edit')")
    @Log(title = "零件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Part part)
    {
        part.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(partService.updateById(part));
    }

    /**
     * 删除零件
     */
    @ApiOperation("删除零件")
    @PreAuthorize("@ss.hasPermi('part:part:remove')")
    @Log(title = "零件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{partids}")
    public AjaxResult remove(@PathVariable List<Long> partids)
    {
        return toAjax(partService.removeByIds(partids));
    }
}
